/*
 * bw_TsMux.cpp
 *
 *  Created on: 2018年11月9日
 *      Author: linsainan
 */

#include "bw_TsMux.h"
#include "public.h"
#include "cJSON.h"
#include "bw_FcgiRequest.h"
#include "bw_jskey.h"
#include "bw_sys.h"

using namespace bwcgi;

extern char g_outbuf[];
extern cJSON *retMesgHead(cJSON *root,unsigned int retcode,const char *retmsg);

// 复用通用信息设置
const char* general_set(const char* json) {
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	cJSON* root = cJSON_Parse(json);
	if (!root) {
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) {
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// max_bit_rate
	cJSON* p_tmp = cJSON_GetObjectItem(param, "max_bit_rate");
	if (!p_tmp || p_tmp->valueint <= 0) {
		cgi_debug("max_bit_rate error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int max_bit_rate = p_tmp->valueint;

	// org_nt_id
	p_tmp = cJSON_GetObjectItem(param, "org_nt_id");
	if (!p_tmp || p_tmp->valueint <= 0) {
		cgi_debug("org_nt_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int org_nt_id = p_tmp->valueint;

	// nt_id
	p_tmp = cJSON_GetObjectItem(param, "nt_id");
	if (!p_tmp || p_tmp->valueint <= 0) {
		cgi_debug("nt_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int nt_id = p_tmp->valueint;

	// trans_id
	p_tmp = cJSON_GetObjectItem(param, "trans_id");
	if (!p_tmp || p_tmp->valueint <= 0) {
		cgi_debug("trans_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int trans_id = p_tmp->valueint;

	// pcr_crc
	p_tmp = cJSON_GetObjectItem(param, "pcr_crc");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0) {
		cgi_debug("pcr_crc error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int pcr_crc = std::stoi(p_tmp->valuestring);
	if (pcr_crc < BWENC_TXMUX_PCRCRC_DIRECT
			|| pcr_crc > BWENC_TXMUX_PCRCRC_REGEN) {
		cgi_debug("pcr_crc value error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	sem_wait(g_shrmsem);
	stENC_MUXSET* mux_info = &(g_encset_mst->enc_mptsmux);
	mux_info->mux_maxrate_kb = max_bit_rate;
	mux_info->mux_origin_nid = org_nt_id;
	mux_info->mux_netid = nt_id;
	mux_info->mux_transid = trans_id;
	mux_info->mux_pcr_correct = pcr_crc;
	g_encset_mst->enc_setvsion++;
	sem_post(g_shrmsem);

	cJSON_Delete(root);
	log_write(BWENC_LOG_SYS_SET, "Ts Mux General info modified successfully");
	return bwenc_common_ack(BWENC_ERROR_NONE);
}

// 复用通用信息获取
const char* general_get() {
	cJSON *pout_root = NULL;
	cJSON *p_obj = NULL;
	stENC_MUXSET* mux_info = NULL;

	pout_root = cJSON_CreateObject();
	cJSON_AddNumberToObject(pout_root, "retCode", 0);
	cJSON_AddStringToObject(pout_root, "retMsg", "SUCCESS");
	p_obj = cJSON_CreateObject();
	cJSON_AddItemToObject(pout_root, "retData", p_obj);

	mux_info = &(g_encset_mst->enc_mptsmux);
	cJSON_AddNumberToObject(p_obj, "max_bit_rate", mux_info->mux_maxrate_kb);
	cJSON_AddNumberToObject(p_obj, "org_nt_id", mux_info->mux_origin_nid);
	cJSON_AddNumberToObject(p_obj, "nt_id", mux_info->mux_netid);
	cJSON_AddNumberToObject(p_obj, "trans_id", mux_info->mux_transid);
	char pcr[1] ={'\0'};
	sprintf(pcr,"%d",mux_info->mux_pcr_correct);
	cJSON_AddStringToObject(p_obj, "pcr_crc", pcr);

	char* str = cJSON_PrintUnformatted(pout_root);
	memset(g_outbuf, 0, get_g_oubuf_size());
	memcpy(g_outbuf, str, strlen(str));
	free(str);

	cJSON_Delete(pout_root);
	return g_outbuf;
}

// TODO 输出复用节目信息获取
int getOutputPrg()
{
	return 1;
}

const char* prg_list_response(PAGE_QUERY query) 
{
	cJSON *pout_root = NULL;
	cJSON *p_data = NULL;
	cJSON *p_tmp = NULL;
	cJSON *pret_array = NULL;
	int i = 0;
	int pos = 0;
	char *p_backet=NULL;
	char tmpbuf[50];
	MUX_PRG_INFO prginfo[32];
	MUX_PRG_INFO *item = NULL;
	
	int len = 0;

	//获取复用界面列表；
	memset(prginfo,0,sizeof(prginfo));
	len = getOutputPrg();

	//构建返回网页的json；
	pout_root = cJSON_CreateObject();
	p_data = retMesgHead(pout_root,0,"SUCCESS");

	sprintf(tmpbuf,"%d",len);
	cJSON_AddStringToObject( p_data, "total", tmpbuf);

	//数组
	pret_array = cJSON_CreateArray();
	cJSON_AddItemToObject( p_data, "program_list", pret_array);

	for(i =0; i < query.count; i++)
	{
		pos = i + query.count * (query.page - 1);
		
		if(pos >= len || pos >= 32)
		{
			break;
		}
		
		p_tmp = cJSON_CreateObject();
		cJSON_AddItemToArray(pret_array, p_tmp);

		item = &(prginfo[pos]);

		sprintf(tmpbuf,"%d",item->index);
		cJSON_AddStringToObject( p_tmp, "id", tmpbuf);

		snprintf(tmpbuf,sizeof(tmpbuf),"%s",item->service_name);
		cJSON_AddStringToObject( p_tmp, "srv_name", tmpbuf);

		snprintf(tmpbuf,sizeof(tmpbuf),"%s",item->provider_name);
		cJSON_AddStringToObject( p_tmp, "srv_provider", tmpbuf);

		sprintf(tmpbuf,"%x",item->service_id);
		cJSON_AddStringToObject( p_tmp, "srv_id", tmpbuf);

		sprintf(tmpbuf,"%x",item->pmt_pid);
		cJSON_AddStringToObject( p_tmp, "org_pmt_pid", tmpbuf);

		sprintf(tmpbuf,"%x",item->pmt_pid_map);
		cJSON_AddStringToObject( p_tmp, "map_pmt_pid", tmpbuf);
		
		sprintf(tmpbuf,"%x",item->pcr_pid);
		cJSON_AddStringToObject( p_tmp, "org_pcr_pid", tmpbuf);

		sprintf(tmpbuf,"%x",item->pcr_pid_map);
		cJSON_AddStringToObject( p_tmp, "map_pcr_pid", tmpbuf);

		sprintf(tmpbuf,"%x",item->v_pid);
		cJSON_AddStringToObject( p_tmp, "org_vd_pid", tmpbuf);

		sprintf(tmpbuf,"%x",item->v_pid_map);
		cJSON_AddStringToObject( p_tmp, "map_vd_pid", tmpbuf);

		sprintf(tmpbuf,"%x",item->a_pid);
		cJSON_AddStringToObject( p_tmp, "org_ad_pid", tmpbuf);
		sprintf(tmpbuf,"%x",item->a_pid_map);
		cJSON_AddStringToObject( p_tmp, "map_ad_pid", tmpbuf);

		sprintf(tmpbuf,"%d",item->pcr_interval);
		cJSON_AddStringToObject( p_tmp, "pcr_interval", tmpbuf);
		
		sprintf(tmpbuf,"%d",item->mux_bitrate);
		cJSON_AddStringToObject( p_tmp, "ts_bitrate", tmpbuf);
	}

	p_backet = cJSON_PrintUnformatted(pout_root);
	memset(g_outbuf,0,get_g_oubuf_size());
	strncpy(g_outbuf, p_backet, strlen(p_backet));
	
	free(p_backet);
	cJSON_Delete(pout_root);

	return g_outbuf;	
}

const char* pgm_list(const char *json) 
{
	cJSON *p_root = NULL;
	cJSON *p_para = NULL;
	cJSON *p_tmp = NULL;
	eENC_ERROR code = BWENC_ERROR_NONE;
	PAGE_QUERY query;

	p_root = cJSON_Parse(json);
	if (p_root == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_para = cJSON_GetObjectItem(p_root, "param");
	if(p_para == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	memset(&query,0,sizeof(query));
	p_tmp = cJSON_GetObjectItem(p_para, "page");
	if(NULL != p_tmp)
	{
		query.page = atoi(p_tmp->valuestring);
	}
	else
	{
		cgi_debug("parse pase error");
		code = BWENC_ERROR_INVALID_PARAM;
		goto error;
	}

	query.page = query.page < 1 ? 1 : query.page;
	p_tmp = cJSON_GetObjectItem(p_para, "count");
	if(NULL != p_tmp)
	{
		query.count = atoi(p_tmp->valuestring);
	}
	else
	{
		cgi_debug("parse count error");
		code = BWENC_ERROR_INVALID_PARAM;
		goto error;
	}
	
	cJSON_Delete(p_root);	
	
	return prg_list_response(query);
	
error:		
	cJSON_Delete(p_root);
	return bwenc_common_ack(code);
}


/************************************************************************************/
// TODO 节目表-获取可选的输入节目列表
/************************************************************************************/
int getInputPrgInfo()
{
	return 10;
}

const char *input_prg_list_response()
{
	cJSON *pout_root = NULL;
	cJSON *p_data = NULL;
	cJSON *p_chl = NULL;
	cJSON *p_tmp = NULL;
	cJSON *pret_array = NULL;

	cJSON *p_array_prg = NULL;
	
	int i = 0;
	unsigned int j = 0;
	int pos = 0;
	char *p_backet=NULL;
	char tmpbuf[50];
	InputChInfo inputchinfo[24 + 32];
	InputChInfo *item = NULL;
	
	int len = 0;

	memset(&inputchinfo,0,sizeof(inputchinfo));
	//获取输入节目列表；
	len = getInputPrgInfo();

	//构建返回网页的json；
	pout_root = cJSON_CreateObject();
	p_data = retMesgHead(pout_root,0,"SUCCESS");

	sprintf(tmpbuf,"%d",len);
	cJSON_AddStringToObject( p_data, "total", tmpbuf);

	//数组
	pret_array = cJSON_CreateArray();
	cJSON_AddItemToObject( p_data, "ts_list", pret_array);

	for(i =0; i < len; i++)
	{
		p_chl = cJSON_CreateObject();
		cJSON_AddItemToArray(pret_array, p_chl);

		item = &(inputchinfo[pos]);
		
		sprintf(tmpbuf,"%d",item->chl_index);
		cJSON_AddStringToObject( p_chl, "chl_id", tmpbuf);

		cJSON_AddStringToObject( p_chl, "chl_name", item->chl_name);
		if(item->prg_total < 0 || item->prg_total > 32)
		{
			continue;
		}

		//数组
		p_array_prg = cJSON_CreateArray();
		cJSON_AddItemToObject( p_chl, "prg_info", p_array_prg);
		for(j = 0; j < item->prg_total; j++)
		{
			p_tmp = cJSON_CreateObject();
			cJSON_AddItemToArray(p_array_prg, p_tmp);

			sprintf(tmpbuf,"%d",item->inputprg[j].prg_index);
			cJSON_AddStringToObject( p_tmp, "prg_index", tmpbuf);

			snprintf(tmpbuf,sizeof(tmpbuf),"%s",item->inputprg[j].prg_name);
			cJSON_AddStringToObject( p_tmp, "prg_name", tmpbuf);

			snprintf(tmpbuf,sizeof(tmpbuf),"%d",item->inputprg[j].is_mux);
			cJSON_AddStringToObject( p_tmp, "mux_status", tmpbuf);
		}
	}

	p_backet = cJSON_PrintUnformatted(pout_root);
	memset(g_outbuf,0,get_g_oubuf_size());
	strncpy(g_outbuf, p_backet, strlen(p_backet));

	free(p_backet);
	cJSON_Delete(pout_root);

	return g_outbuf;	
}

//输入节目 选择列表
const char* pgm_sel_list(const char* json) 
{
	cJSON *p_root = NULL;
	cJSON *p_para = NULL;
	//cJSON *p_tmp = NULL;
	eENC_ERROR code = BWENC_ERROR_NONE;
	//PAGE_QUERY query;

	p_root = cJSON_Parse(json);
	if (p_root == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_para = cJSON_GetObjectItem(p_root, "param");
	if(p_para == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}
	
	cJSON_Delete(p_root);	
	
	return input_prg_list_response();
	
error:		
	cJSON_Delete(p_root);
	return bwenc_common_ack(code);
}



/**********************************************************************************************/
// TODO 输入节目解析
int parseInputChanel(int ch_no)
{
	return 0;
}

const char* pgm_parse_prg(const char* json) 
{
	cJSON *p_root = NULL;
	cJSON *p_para = NULL;
	cJSON *p_tmp = NULL;
	eENC_ERROR code = BWENC_ERROR_NONE;
	int input_ch = 0;

	p_root = cJSON_Parse(json);
	if (p_root == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_para = cJSON_GetObjectItem(p_root, "param");
	if(p_para == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_tmp = cJSON_GetObjectItem(p_para, "input_ch");
	if(NULL != p_tmp)
	{
		input_ch = atoi(p_tmp->valuestring);
	}

	cgi_debug("parse ch :%d",input_ch);
	parseInputChanel(input_ch);
	
error:		
	cJSON_Delete(p_root);
	return bwenc_common_ack(code);	
}


//**************************************************************//
// TODO 节目复用选择, 添加
//**************************************************************//
int SelectProgramMux()
{
	return 0;
}

const char* pgm_muxselect(const char* json) 
{
	cJSON *p_root = NULL;
	cJSON *p_para = NULL;
	cJSON *p_tmp = NULL;
	cJSON *p_array = NULL;
	eENC_ERROR code = BWENC_ERROR_NONE;
	//int input_ch_index = 0;
	MuxAdd addmux[32];
	int set_num = 0;
	int i = 0;

	p_root = cJSON_Parse(json);
	if (p_root == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_para = cJSON_GetObjectItem(p_root, "param");
	if(p_para == NULL)
	{
		cgi_debug("parse para error");
		goto error;
	}

	p_array = cJSON_GetObjectItem(p_para, "mux");
	set_num  = cJSON_GetArraySize(p_array);
	
	memset(&addmux,0,sizeof(addmux));

	if(set_num < 0 || set_num > 32)
	{
		code = BWENC_ERROR_INVALID_PARAM;
		goto error;
	}

	for(i = 0; i < set_num; i++)
	{
		p_tmp = cJSON_GetObjectItem(p_para, "input_ch");
		if(NULL != p_tmp)
		{
			addmux[i].input_ch = atoi(p_tmp->valuestring);
		}

		p_tmp = cJSON_GetObjectItem(p_para, "input_prg_ch");
		if(NULL != p_tmp)
		{
			addmux[i].input_prg_ch = atoi(p_tmp->valuestring);
		}

		p_tmp = cJSON_GetObjectItem(p_para, "output_ch");
		if(NULL != p_tmp)
		{
			addmux[i].output_ch = atoi(p_tmp->valuestring);
		}
	}

	for(i = 0; i < set_num; i++)
	{
		cgi_debug("input_ch:%d,input_prg_ch:%d,out_ch:%d",addmux[i].input_ch,addmux[i].input_prg_ch,addmux[i].output_ch);
	}
	SelectProgramMux();
	
error:		
	cJSON_Delete(p_root);
	return bwenc_common_ack(code);	
}


//复用节目信息修改；
